package com.onlinefurnitureshop.management.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.onlinefurnitureshop.management.model.Resource;
import com.onlinefurnitureshop.management.service.ResourceService;
import com.onlinefurnitureshop.management.service.UsersService;

public class IndexAction extends BaseAction{
	private ResourceService resourceService;
	private UsersService usersService;
	private List<Resource> menuList;
	private Map<Resource, List<Resource>> menuMap;
	
	
	
	
	public Map<Resource, List<Resource>> getMenuMap() {
		return menuMap;
	}




	public void setMenuMap(Map<Resource, List<Resource>> menuMap) {
		this.menuMap = menuMap;
	}




	public ResourceService getResourceService() {
		return resourceService;
	}




	public void setResourceService(ResourceService resourceService) {
		this.resourceService = resourceService;
	}




	public UsersService getUsersService() {
		return usersService;
	}




	public void setUsersService(UsersService usersService) {
		this.usersService = usersService;
	}




	public List<Resource> getMenuList() {
		return menuList;
	}




	public void setMenuList(List<Resource> menuList) {
		this.menuList = menuList;
	}




	public String index(){
		String curretUser = (String) subject().getPrincipal();
		System.out.println("currnetUser:"+curretUser);
		Set<String> permissions = usersService.findPermissions(curretUser);
		System.out.println("permission:"+permissions);
		menuList = resourceService.findMenus(permissions);
		List<Resource> descendants = null ;
		menuMap = new HashMap<>();
		for(Resource menu:menuList){
			if(menu.isRootNode()){
				descendants = new ArrayList<>();
				menuMap.put(menu, descendants);
			}else{
				descendants.add(menu);
			}
		}
		System.out.println(menuMap);
		return "index";
	}
	

}
